﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Pessoas_Relatorios_prAdvertencias : PageBase 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CarregaComboTipoSuspensao(rcbTipoSuspensao);
            OcultaCampos(true);
            rdpPeriodoInicial.SelectedDate = DateTime.Parse("01/" + DateTime.Today.Month.ToString() + "/" + DateTime.Today.Year.ToString());
            rdpPeriodoFinal.SelectedDate = DateTime.Parse("30/" + DateTime.Today.Month.ToString() + "/" + DateTime.Today.Year.ToString());
        }
    }

    #region Eventos
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        try
        {
            Pesquisa();
            rgAdvertencias.DataBind();
            OcultaCampos(false);
        }
        catch (Exception ex)
        {
            lblResultado.Text = ex.Message;
        }
    }

    protected void btnLimpar_Click(object sender, EventArgs e)
    {
        foreach (RadComboBoxItem item in rcbTipoSuspensao.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chk1");
            chk.Checked = true;
        }
        rcbTipoSuspensao.SelectedIndex = 0;
        rdpPeriodoInicial.SelectedDate = DateTime.Parse("01/" + DateTime.Today.Month.ToString() + "/" + DateTime.Today.Year.ToString());
        rdpPeriodoFinal.SelectedDate = DateTime.Parse("30/" + DateTime.Today.Month.ToString() + "/" + DateTime.Today.Year.ToString());

        OcultaCampos(true);
    }

    protected void rgAdvertencias_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        Pesquisa();
    }

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        Pesquisa();
        rgAdvertencias.DataBind();
    }
    #endregion

    #region Metodos
    private void CarregaComboTipoSuspensao(RadComboBox combo)
    {
        TipoSuspensao tiposuspensao = new TipoSuspensao();
        combo.DataSource = tiposuspensao.SelectAll();
        combo.DataBind();
    }

    private void OcultaCampos(bool oculta)
    {
        trResultado.Visible = !oculta;
    }

    private void Pesquisa()
    {
        
        rgAdvertencias.DataSource = CarregaAdvertencias();
    }

    private List<PessoaAdvertencia> CarregaAdvertencias()
    {
        PessoaAdvertencia advertencia = new PessoaAdvertencia();
        List<PessoaAdvertencia> listAdvertencias = new List<PessoaAdvertencia>();

        List<int> listTipoSuspensao = new List<int>();
        foreach (RadComboBoxItem item in rcbTipoSuspensao.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chk1");
            if (chk.Checked)
                listTipoSuspensao.Add(int.Parse(item.Value));
        }

        int[] _tiposuspensoes = new int[listTipoSuspensao.Count];
        for (int i = 0; i < listTipoSuspensao.Count; i++)
        {
            _tiposuspensoes[i] = listTipoSuspensao[i];
        }

        DateTime datainicial = DateTime.Parse(rdpPeriodoInicial.SelectedDate.ToString());
        DateTime datafinal = DateTime.Parse(rdpPeriodoFinal.SelectedDate.ToString());

        //listAdvertencias = advertencia.SelectAdvertencias(_tiposuspensoes, datainicial, datafinal, true, true, true);

        return listAdvertencias;
    }
    #endregion
}
